﻿Imports DevExpress.XtraEditors
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO

Public Class FormIdentitas1
    Private constparam() As String = {"nama", "alamat", "kota", "kepalasekolah", "keterangan", "logosekolah"}
    Private Function imagetobyte(ByVal imageBox As Control, ByVal strGambar As String) As Byte()
        Dim imageArray As Byte()
        If strGambar = "" Then
            Dim originalImage As Image
            originalImage = Nothing
            If TypeOf imageBox Is PictureBox Then
                Dim boximg As New PictureBox
                boximg = CType(imageBox, PictureBox)
                originalImage = boximg.Image
            ElseIf TypeOf imageBox Is ImageEdit Then
                Dim boximg As New ImageEdit
                boximg = CType(imageBox, ImageEdit)
                originalImage = boximg.Image
            End If

            If originalImage IsNot Nothing Then
                Using stream As IO.MemoryStream = New IO.MemoryStream()
                    originalImage.Save(stream, Imaging.ImageFormat.Jpeg)
                    imageArray = stream.ToArray
                End Using

            Else
                imageArray = Nothing
            End If
        Else
            imageArray = IO.File.ReadAllBytes(strGambar)
        End If
        Return imageArray
    End Function
    Private Sub Byte2Image(ByRef NewImage As Image, ByVal ByteArr() As Byte)
        '
        Dim ImageStream As MemoryStream
        Try

            If ByteArr.GetUpperBound(0) > 0 Then
                ImageStream = New MemoryStream(ByteArr)
                NewImage = Image.FromStream(ImageStream)
            Else
                NewImage = Nothing
            End If

        Catch ex As Exception

            NewImage = Nothing

        End Try

    End Sub
    Private Function setparam() As Object()
        Dim param As New List(Of Object)
        param.Clear()
        For Each Sctl As String In constparam
            Dim ctl As Control()
            ctl = Me.Controls.Find(Sctl, True)
            If ctl IsNot Nothing And ctl.Length > 0 Then
                If TypeOf ctl(0) Is MemoEdit Then
                    Dim ctlmemo As New MemoEdit
                    ctlmemo = CType(ctl(0), MemoEdit)
                    Dim alamatset As String
                    Dim bf As New BinaryFormatter
                    Dim ms As New IO.MemoryStream
                    Dim bytearr As Byte()
                    If ctlmemo.Lines.Count > 0 Then
                        ms = New MemoryStream
                        bf.Serialize(ms, ctlmemo.Lines)
                        bytearr = ms.ToArray
                        Dim vmemo As String = String.Empty
                        For i As Integer = 0 To ctlmemo.Lines.Count - 1
                            vmemo += ctlmemo.Lines(i).ToString
                            If i < ctlmemo.Lines.Count - 1 Then
                                vmemo += "\n"
                            End If
                        Next
                        alamatset = vmemo
                        param.Add(alamatset)
                    Else
                        param.Add(Convert.DBNull)
                    End If
                ElseIf TypeOf ctl(0) Is TextEdit Then
                    If ctl(0).Text = "" Then
                        param.Add(Convert.DBNull)
                    Else
                        param.Add(ctl(0).Text)
                    End If
                ElseIf TypeOf ctl(0) Is PictureBox Then
                    Dim ctlimage As New PictureBox
                    ctlimage = CType(ctl(0), PictureBox)
                    Dim imageArray As Byte()
                    If ctlimage.ImageLocation = "" Then
                        imageArray = imagetobyte(ctlimage, filename)
                        If imageArray Is Nothing Then
                            param.Add(Convert.DBNull)
                        Else
                            param.Add(imageArray)
                        End If
                    Else
                        param.Add(IO.File.ReadAllBytes(filename))
                    End If

                    
                End If
            End If
        Next
        Return param.ToArray
    End Function
    Private Sub FillData(ByVal dtin As DataTable)
        For Each Sctl As String In constparam
            Dim ctl As Control()
            ctl = Me.Controls.Find(Sctl, True)
            If ctl IsNot Nothing And ctl.Length > 0 Then
                If TypeOf ctl(0) Is MemoEdit Then
                    Dim ctlmemo As New MemoEdit
                    ctlmemo = CType(ctl(0), MemoEdit)
                    If IsDBNull(dtin(0)(Sctl)) Then
                        ctlmemo.Text = ""
                    Else
                        Dim rtb As New RichTextBox
                        rtb.Text = dtin(0)(Sctl)
                        ctlmemo.Lines = rtb.Lines
                    End If
                ElseIf TypeOf ctl(0) Is TextEdit Then
                    If IsDBNull(dtin(0)(Sctl)) Then
                        ctl(0).Text = ""
                    Else
                        ctl(0).Text = dtin(0)(Sctl)
                    End If
                ElseIf TypeOf ctl(0) Is PictureBox Then
                    Dim ctlimage As New PictureBox
                    ctlimage = CType(ctl(0), PictureBox)
                    If IsDBNull(dtin(0)(Sctl)) Then
                        ctlimage.Image = Nothing
                    Else
                        Dim img As Image
                        img = Nothing
                        Byte2Image(img, dtin(0)(Sctl))
                        ctlimage.Image = img
                        ctlimage.SizeMode = PictureBoxSizeMode.StretchImage
                    End If
                End If
            End If
        Next
    End Sub

    Private Function validasi() As Boolean
        Dim retval As Boolean = True
        For Each Sctl As String In constparam
            Dim ctl As Control()
            ctl = Me.Controls.Find(Sctl, True)
            If ctl IsNot Nothing And ctl.Length > 0 Then
                If TypeOf ctl(0) Is MemoEdit Then
                    Dim ctlmemo As New MemoEdit
                    ctlmemo = CType(ctl(0), MemoEdit)
                    If ctlmemo.Text = "" Then
                        retval = False
                    End If
                ElseIf TypeOf ctl(0) Is TextEdit Then
                    If ctl(0).Text = "" Then
                        retval = False
                    End If
                End If
            End If
        Next
        Return retval
    End Function

    Private Sub refreshdata()
        Dim dt As New DataTable
        If Utils.executeSP("proc_searchall", New Object() {"*", "dataidentitas", "nama", ""}, dt) Then
            If Utils.RowExist(dt) Then
                FillData(dt)
            End If
        End If
    End Sub
    Private Sub FormIdentitas1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        refreshdata()
    End Sub
    Dim filename As String
    Private Sub SimpleButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton4.Click

        OpenFileDialog1.InitialDirectory = "c:\"
        OpenFileDialog1.Filter = "JPEG (*.jpg) |*.jpg;*.jpe;*.jpeg | PNG (*.png)|*.png;*.png | GIF (*.gif)|*.gif;*.gif | BMP (*.bmp)|*.bmp;*.bmp |All files (*.*)|*.*" '"Word (*.doc) |*.doc;*.rtf|(*.txt) |*.txt|(*.*) |*.*" 

        OpenFileDialog1.FilterIndex = 2
        OpenFileDialog1.RestoreDirectory = True
        OpenFileDialog1.FileName = ""
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            filename = OpenFileDialog1.FileName
            Dim fz As UInt32 = 0
            Dim fs As IO.MemoryStream
            Try

                fs = New IO.MemoryStream(IO.File.ReadAllBytes(filename))
                fz = fs.Length
                If fz > 500000 Then
                    MsgBox("Maaf Maksimal Ukuran File = 1Mb(1.000.000 byte)")
                    Exit Sub
                End If
            Catch ex As Exception

            End Try

            logosekolah.ImageLocation = filename
            logosekolah.SizeMode = PictureBoxSizeMode.StretchImage
            logosekolah.Refresh()
        End If
    End Sub

    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
        If validasi() Then
            Dim dt As New DataTable
            If Utils.executeSP("proc_identitas", setparam, dt) Then
                If Utils.RowExist(dt) Then
                    MsgBox(dt(0)(0), MsgBoxStyle.Information)
                    refreshdata()
                    BaseMain.refreshdata()
                End If
            End If
        Else
            MsgBox("Data Entry Belum Lengkap", MsgBoxStyle.Exclamation)
        End If
    End Sub
End Class
